package org.monkey.common.cbs.util;

import cn.hutool.core.util.StrUtil;
import java.util.function.Predicate;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

public class DocketFactory {
  private static final String PACKAGE_NAME="org.monkey";
  public static Docket build(ApiInfo apiInfo,String groupName,String packageName) {
    Predicate<RequestHandler> pr =null;
    if(StrUtil.isBlank(packageName)){
      pr = RequestHandlerSelectors.basePackage(PACKAGE_NAME);
    }else{
      pr = RequestHandlerSelectors.basePackage(packageName);
    }
    Docket docket = new Docket(DocumentationType.OAS_30)
        .apiInfo(apiInfo)
        .select()
        .apis(pr)
        .paths(PathSelectors.any())
        .build();
    if(StrUtil.isNotBlank(groupName)){
      docket.groupName(groupName);
    }

    return docket;
  }

  public static Docket build(ApiInfo apiInfo,String groupName) {
    Docket docket = new Docket(DocumentationType.OAS_30)
        .apiInfo(apiInfo)
        .select()
        .apis(RequestHandlerSelectors.basePackage(PACKAGE_NAME))
        .paths(PathSelectors.any())
        .build();
    if(StrUtil.isNotBlank(groupName)){
      docket.groupName(groupName);
    }
    return docket;
  }

  public static Docket build(ApiInfo apiInfo) {
    return build(apiInfo,null);
  }
}
